package org.thrj.wechat.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.thrj.wechat.sdk.impl.WeChatCommonSdkImpl;
import org.thrj.wechat.sdk.impl.WeChatPushMessageSdkImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class WeChatSdkLoadReporter implements ApplicationRunner {

    private static final Logger log = LoggerFactory.getLogger(WeChatSdkLoadReporter.class);

    @Autowired
    private ApplicationContext applicationContext;

    private final List<ComponentInfo> components = Arrays.asList(
            new ComponentInfo(
                    WeChatCommonSdkImpl.class,
                    "weChatCommonSdkImpl",
                    "需要配置 wx.appid 和 wx.appSecret"
            ),
            new ComponentInfo(
                    WeChatPushMessageSdkImpl.class,
                    "weChatPushMessageSdkImpl",
                    "需要配置 WeChatCommonSdk"
            )
            // 可继续添加更多
    );

    @Override
    public void run(ApplicationArguments args) {
        List<String> loaded = new ArrayList<>();
        List<String> notLoaded = new ArrayList<>();
        for (ComponentInfo info : components) {
            boolean isLoaded = applicationContext.containsBean(info.beanName);
            String message = info.className + " (" + info.reason + ")";
            if (isLoaded) {
                loaded.add(message);
            } else {
                notLoaded.add(message);
            }
        }
        if (!loaded.isEmpty()) {
            log.info("以下组件已加载：\n   - {}", String.join("\n   - ", loaded));
        }
        if (!notLoaded.isEmpty()) {
            log.info("ℹ以下组件因条件不满足未加载：\n   - {}", String.join("\n   - ", notLoaded));
        }
    }

}
